#include "usb/keyboard.h"


namespace iecas_ns
{
    shared_ptr<CKeyboard> CKeyboard::sm_intancePtr = nullptr;
    const char chr_table[129] = {
    [0] = '\0', [1] = '*', [2] = '1', [3] = '2', [4] = '3', 
    [5] = '4', [6] = '5', [7] = '6', [8] = '7', [9] = '8',
    [10] = '9',[11] = '0', [12] = '-', [13] = '=', [14] = '\b', 
    [15] = '\t', [16] = 'q', [17] = 'w', [18] = 'e', [19] = 'r',
    [20] = 't', [21] = 'y', [22] = 'u', [23] = 'i', [24] = 'o', 
    [25] = 'p', [26] = '[', [27] = ']', [28] = '\n', [29] = '*',
    [30] = 'a', [31] = 's', [32] = 'd', [33] = 'f', [34] = 'g', 
    [35] = 'h', [36] = 'j', [37] = 'k', [38] = 'l', [39] = ';',
    [40] = '\'', [41] = '`', [42] = '*', [43] = '\\',
    [44] = 'z', [45] = 'x', [46] = 'c', [47] = 'v', [48] = 'b', 
    [49] = 'n', [50] = 'm', [51] = ',', [52] = '.', [53] = '/', 
    [54] = '*', [55] = '*', [56] = '*', [57] = ' ', [58] = '*', 
    [59] = '*', [60] = '*', [61] = '*', [62] = '*', [63] = '*',
    [64] = '*', [65] = '*', [66] = '*', [67] = '*', [68] = '*', 
    [69] = '*', [70] = '*', [71] = '*', [72] = '*', [73] = '*',
    [74] = '*', [75] = '*', [76] = '*', [77] = '*', [78] = '*', [79] = '*',
    [80] = '*', [81] = '*', [82] = '*', [83] = '*', [84] = '*',
    [85] = '*', [86] = '*', [87] = '*', [88] = '*', [89] = '*', [90] = '*', 
    [91] = '*', [92] = '*', [93] = '*', [94] = '*', [95] = '*', 
    [96] = '*', [97] = '*', [98] = '*', [99] = '*', [100] = '*', 
    [101] = '*', [102] = '*', [103] = '*', [104] = '*', [105] = '*',
    [106] = '*', [107] = '*', [108] = '*',[109] = '*', [110] = '*',
    [111] = '*', [112] = '*', [113] = '*', [114] = '*', [115] = '*',
    [116] = '*', [117] = '*', [118] = '*', [119] = '*', [120] = '*',
    [121] = '*', [122] = '*', [123] = '*', [124] = '*', [125] = '*', 
    [126] = '*', [127] = '*', [128] = '*'
    };

    CKeyboard::CKeyboard()
    {
        m_commonPtr = shared_ptr<CCommon>(CCommon::getInstance());
        m_srcDev = "";
    }

	shared_ptr<CKeyboard> CKeyboard::getInstance()
	{
		if(sm_intancePtr == nullptr)
		{
			sm_intancePtr = shared_ptr<CKeyboard>(new CKeyboard());
		}
		return sm_intancePtr;
	}

    bool CKeyboard::init(string srcDev)
    {
        int fd;

        fd = open(srcDev.c_str(), O_RDONLY);
        if(fd <= 0)
        {
            cout << "keyboard init " << srcDev << " faild, fd " << fd << endl;
            PLOGD << "keyboard init " << srcDev << " faild, fd " << fd;
            return false;
        }
        else
        {
            cout << "keyboard init " << srcDev << " success, fd " << fd << endl;
            PLOGD << "keyboard init " << srcDev << " success, fd " << fd;
            m_commonPtr->addDev(srcDev, fd);
            m_srcDev = srcDev;
            return true;
        }        
    }

    void CKeyboard::start(void)
    {
        thread tempThread(&CKeyboard::loop, this);
        tempThread.detach();
    }

    void CKeyboard::loop(void)
    {
        int fd = -1, fd_last = -1, fs_sel;
        char ch;
        struct input_event t;
        Value data;
        fd_set fs_read;
        struct timeval time;

        time.tv_sec = 0;
        time.tv_usec = 1000;

        while(true)
        {
            if(m_commonPtr->getDevFdStat(m_srcDev, fd))
            {
                fd_last = fd;
                FD_ZERO(&fs_read);
                FD_SET(fd, &fs_read);
                //使用select实现多路通信
                fs_sel = select(fd + 1, &fs_read, NULL, NULL, &time);
                if(fs_sel > 0)
                {
                    read(fd, &t, sizeof(t));
                    if((t.type == EV_KEY) && (t.value == 0))
                    {
                        ch = chr_table[t.code];
                        // cout << "keyboard get raw " << t.code << endl;
                        // PLOGD << "keyboard get raw " << t.code;           
                        // cout << "keyboard get char " << ch << endl;
                        // PLOGD << "keyboard get char " << ch;
                        data[SRC_DEV] = m_srcDev;
                        string ss(1, ch);
                        data[SRC_DATA] = ss;
                        cout << "keyboard data add dev: " << data[SRC_DEV].asString() << " data: " << data[SRC_DATA].asString() << endl;
                        PLOGD << "keyboard data add dev: " << data[SRC_DEV].asString() << " data: " << data[SRC_DATA].asString();
                        m_commonPtr->addDataBack(data);
                    }
                }
            }
            else
            {
                cout << "keyboard exit port " << m_srcDev << " with fd " << fd_last << endl;
                PLOGD << "keyboard exit port " << m_srcDev << " with fd " << fd_last;
                close(fd_last);
                break;
            }
        }
    }

    bool CKeyboard::deinit(string srcDev)
    {
        m_commonPtr->delDev(srcDev);

        return true;
    }
}
